package com.duobaoyu.dby.supply.biz.client.boundary.order;

import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson.JSON;
import com.duobaoyu.dby.channel.adapter.enums.biz.config.GlobalErrorCodeEnum;
import com.duobaoyu.dby.channel.adapter.enums.biz.order.OrderStatusEnum;
import com.duobaoyu.dby.channel.boundary.bean.order.param.*;
import com.duobaoyu.dby.channel.boundary.bean.order.result.*;
import com.duobaoyu.dby.channel.boundary.template.SupplyChannelTemplate;
import com.duobaoyu.dby.common.mybatis.util.EnumUtils;
import com.duobaoyu.dby.supply.biz.client.boundary.order.bean.bo.*;
import com.duobaoyu.dby.supply.biz.client.product.bean.bo.SkuInfoCheckBO;
import com.duobaoyu.dby.supply.biz.client.product.converter.ProductConverter;
import com.duobaoyu.dby.supply.biz.converter.OrderConverter;
import com.duobaoyu.dby.supply.common.biz.bo.area.ConsigneeInfo;
import com.duobaoyu.dby.supply.common.biz.bo.order.*;
import com.duobaoyu.dby.supply.common.biz.remote.dto.order.SubmitOrderDTO;
import com.duobaoyu.dby.supply.core.common.exception.CustomAssert;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 渠道订单聚合策略
 *
 * @author fengshaoyu
 * @version 1.0.0
 * @date 2023/9/26 9:15
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class OrderBoundaryClient {

    private final SupplyChannelTemplate supplyChannelTemplate;
    private final OrderConverter orderConverter;
    private final ProductConverter productConverter;

	/**
	 * 预提交订单
	 *
	 * @param orderSplit 拆单信息
	 */
	public OrderSubmitBO submitOrder(OrderSplitBO orderSplit,String orderRemark) {
		OrderSubmitParam orderSubmitParam = new OrderSubmitParam();
		orderSubmitParam.setOrderInfoList(this.buildSubmitOrderInfoParam(orderSplit, orderRemark));
		List<OrderSubmitResult> orderSubmitResultList = supplyChannelTemplate.submitChannelOrder(orderSubmitParam);
		OrderSubmitBO orderSubmit = new OrderSubmitBO();
		// 提交失败的订单
		List<OrderSubmitResult> failureOrderList = orderSubmitResultList.stream().filter(item -> !Optional.ofNullable(item).map(OrderSubmitResult::getSuccess).orElse(Boolean.FALSE)).collect(Collectors.toList());
		if(CollectionUtils.isNotEmpty(failureOrderList)) {
			List<OrderSubmitBO.ErrorOrderInfo> errorSkuInfoList = failureOrderList.stream().map(orderSubmitResult -> {
				OrderSubmitBO.ErrorOrderInfo errorOrderInfo = new OrderSubmitBO.ErrorOrderInfo();
				errorOrderInfo.setErrorCode(NumberUtil.isInteger(orderSubmitResult.getErrorCode()) ? Integer.parseInt(orderSubmitResult.getErrorCode()) : GlobalErrorCodeEnum.CHANNEL_ERROR.getCode());
				errorOrderInfo.setErrorMessage(orderSubmitResult.getErrorMessage());
				errorOrderInfo.setErrorSkuCode(orderSubmitResult.getErrorSkuCodeList());
				errorOrderInfo.setErrorData(orderSubmitResult.getData());
				return errorOrderInfo;
			}).collect(Collectors.toList());
			orderSubmit.setErrorOrderList(errorSkuInfoList);
		}
		// 提交成功的订单
		List<OrderSubmitResult.OrderInfo> submitSuccessOrderList = orderSubmitResultList.stream()
				.filter(item -> Optional.ofNullable(item).map(OrderSubmitResult::getSuccess).orElse(Boolean.FALSE))
				.map(OrderSubmitResult::getOrderInfoList)
				.filter(Objects::nonNull)
				.flatMap(Collection::stream)
				.collect(Collectors.toList());
		orderSubmit.setOrderInfoList(orderConverter.toSubmitOrderInfoList(submitSuccessOrderList));
		//请求完后数据补充
		this.mergeOrderInfo(orderSplit, orderSubmit);
		return orderSubmit;
	}

	/**
     * 确认订单
     *
     * @return
     */
    public OrderConfirmResultBO confirmPreOrder(List<OrderBO> orderList) {
        checkOrderSn(orderList, OrderBO::getOrderSn, OrderBO::getOrderTradeSn);
        List<OrderConfirmParam> orderConfirmParamList = orderList.stream().map(orderConverter::toOrderConfirmParam).collect(Collectors.toList());
        List<OrderConfirmResult> orderConfirmResults = supplyChannelTemplate.confirmChannelPreOrder(orderConfirmParamList);
		return buildOrderConfirmResult(orderList, orderConfirmResults);
	}

    /**
     * 提交和确认订单
     */
    public OrderConfirmResultBO submitAndConfirmOrder(List<OrderBO> orderList) {
        checkOrderSn(orderList, OrderBO::getOrderSn, OrderBO::getOrderTradeSn);
        List<OrderConfirmParam> orderConfirmParamList = orderList.stream().map(orderConverter::toOrderConfirmParam).collect(Collectors.toList());
        List<OrderConfirmResult> orderConfirmResults = supplyChannelTemplate.submitAndConfirmChannelOrder(orderConfirmParamList);
		return buildOrderConfirmResult(orderList, orderConfirmResults);

	}


	/**
     * 获取拆单信息
     *
     * @param submitOrder  提交信息
     * @param skuInfoCheck 商品扩展信息
     * @return OrderSplitBO 拆单返回结果
     */
    public OrderSplitBO orderSplit(SubmitOrderDTO submitOrder, SkuInfoCheckBO skuInfoCheck) {
        OrderSplitParam orderSplitParam = new OrderSplitParam();
		orderSplitParam.setBizCode(submitOrder.getShowServerName());
		orderSplitParam.setRechargeAccount(submitOrder.getRechargeAccount());
		orderSplitParam.setAddressInfoParam(orderConverter.toAddressInfoParam(submitOrder.getConsigneeInfo()));
		orderSplitParam.setRecipientInfoParam(orderConverter.toRecipientInfoParam(submitOrder.getRecipientInfo()));
		orderSplitParam.setElectricityFeesInfoParam(orderConverter.toElectricityFeesInfoParam(submitOrder.getElectricityFeesInfo()));

		List<OrderSplitParam.OrderSplitSkuInfo> orderSplitSkuInfoList = submitOrder.getSkuInfos().stream().map(item -> {
			OrderSplitParam.OrderSplitSkuInfo orderSplitSkuInfo = new OrderSplitParam.OrderSplitSkuInfo();
			SkuInfoCheckBO.SkuInfo skuInfo = skuInfoCheck.getSkuInfoList()
					.stream()
					.filter(sku -> Objects.equals(item.getSkuCode(),sku.getSkuCode()))
					.findFirst()
					.orElse(null);
			//地址信息
			orderSplitSkuInfo.setRechargeAccount(item.getRechargeAccount());
			orderSplitSkuInfo.setAddressInfoParam(orderConverter.toAddressInfoParam(item.getConsigneeInfo()));
			orderSplitSkuInfo.setRecipientInfoParam(orderConverter.toRecipientInfoParam(item.getRecipientInfo()));
			orderSplitSkuInfo.setElectricityFeesInfoParam(orderConverter.toElectricityFeesInfoParam(item.getElectricityFeesInfo()));
			CustomAssert.notNull(skuInfo, "商品信息不能为空");
			orderSplitSkuInfo.setSkuCode(skuInfo.getSkuCode());
			orderSplitSkuInfo.setSkuNum(item.getSkuNum());
			orderSplitSkuInfo.setProductType(skuInfo.getProductType());
			orderSplitSkuInfo.setChannelCode(skuInfo.getChannelCode());
			orderSplitSkuInfo.setChannelSupplierId(skuInfo.getChannelSupplierId());

			orderSplitSkuInfo.setTemplateJson(skuInfo.getTemplateJson());
			orderSplitSkuInfo.setCanInvoice(skuInfo.getCanInvoice());
			return orderSplitSkuInfo;
		}).collect(Collectors.toList());
		orderSplitParam.setOrderSplitSkuInfoList(orderSplitSkuInfoList);
		//进行拆单处理
		OrderSplitResult orderSplitResult = supplyChannelTemplate.splitOrder(orderSplitParam);
		List<OrderSplitBO.OrderSplitSkuInfo> orderSplitList = orderSplitResult.getOrderSkuInfoList().stream().map(item -> {
			OrderSplitBO.OrderSplitSkuInfo orderSplitSkuInfo = orderConverter.toOrderSplit(item);
			// 默认供应链订单号同供应链三方交易单号
			orderSplitSkuInfo.setOrderSn(item.getOrderTradeSn());
			ConsigneeInfo consigneeInfo = orderConverter.toOrderConsigneeInfo(item.getAddressInfoParam());
			RecipientInfo recipientInfo = orderConverter.toRecipientInfo(item.getRecipientInfoParam());
			ElectricityFeesInfo electricityFeesInfo = orderConverter.toElectricityFeesInfo(item.getElectricityFeesInfoParam());

			orderSplitSkuInfo.setConsigneeInfo(consigneeInfo);
			orderSplitSkuInfo.setRecipientInfo(recipientInfo);
			orderSplitSkuInfo.setElectricityFeesInfo(electricityFeesInfo);
			return orderSplitSkuInfo;
		}).collect(Collectors.toList());

		List<OrderSplitBO.OrderSplitSkuInfo> orderSkuInfoList = orderSplitList.stream().peek(orderSplitSkuInfo -> {
			List<OrderSplitBO.SplitSkuInfo> skuInfoList = orderSplitSkuInfo.getSkuInfoList().stream().map(splitSkuInfo -> {
				SkuInfoCheckBO.SkuInfo skuInfo = skuInfoCheck.getSkuInfoList()
						.stream()
						.filter(item -> Objects.equals(splitSkuInfo.getSkuCode(),item.getSkuCode()))
						.findFirst()
						.orElse(null);

				CustomAssert.notNull(skuInfo, "商品信息不能为空");
				OrderSplitBO.SplitSkuInfo skuInfoBo = productConverter.toOrderSplitSplitSkuInfo(skuInfo);
				skuInfoBo.setSkuNum(splitSkuInfo.getSkuNum());
				return skuInfoBo;
			}).collect(Collectors.toList());
			orderSplitSkuInfo.setSkuInfoList(skuInfoList);
		}).collect(Collectors.toList());
		return OrderSplitBO.builder().orderSkuInfoList(orderSkuInfoList).build();
	}

    /**
     * 查询渠道订单明细
     */
    public ChannelOrderDetailOutBO getChannelOrderDetail(ChannelOrderDetailInBO channelOrderDetailIn) {
        checkOrderSn(channelOrderDetailIn, ChannelOrderDetailInBO::getOrderSn, ChannelOrderDetailInBO::getOrderTradeSn);
        OrderInfoParam orderInfoParam = new OrderInfoParam();
        orderInfoParam.setChannelCode(channelOrderDetailIn.getChannelCode());
        orderInfoParam.setOrderProductTypeSet(channelOrderDetailIn.getOrderProductTypeSet());
        orderInfoParam.setChannelTradeOrderSn(channelOrderDetailIn.getChannelTradeOrderSn());
        String orderTradeSn = channelOrderDetailIn.getOrderTradeSn();
        orderInfoParam.setOrderTradeSn(orderTradeSn);
        orderInfoParam.setChannelOrderSn(channelOrderDetailIn.getChannelOrderSn());
        orderInfoParam.setChannelOrderParentSn(channelOrderDetailIn.getChannelOrderParentSn());
        orderInfoParam.setOriginalPrice(channelOrderDetailIn.getOriginalPrice());
        OrderInfoResult orderInfo = supplyChannelTemplate.getOrderInfo(orderInfoParam);
		ChannelOrderDetailOutBO channelOrderDetailOut = orderConverter.toChannelOrderDetailOut(orderInfo);
		channelOrderDetailOut.setOrderSn(channelOrderDetailIn.getOrderSn());
		return channelOrderDetailOut;

    }

    /**
     * 批量取消订单
     */
    public CancelOrderOutBO batchCancelOrder(List<CancelOrderInBO> cancelOrderInList) {
        checkOrderSn(cancelOrderInList, CancelOrderInBO::getOrderSn, CancelOrderInBO::getOrderTradeSn);
        List<OrderCancelParam> cancelOrderParam = orderConverter.toCancelOrderParam(cancelOrderInList);
        List<OrderCancelResult> orderCancelResults = supplyChannelTemplate.cancelOrder(cancelOrderParam);

        // 订单号转换
        Map<String, String> orderSnMapping = cancelOrderInList.stream().collect(Collectors.toMap(CancelOrderInBO::getOrderTradeSn, CancelOrderInBO::getOrderSn));

        List<String> failedOrderSnList = orderCancelResults.stream().filter(item -> !Optional.ofNullable(item).map(OrderCancelResult::getSuccess).orElse(Boolean.FALSE)).map(OrderCancelResult::getOrderTradeSn).map(orderSnMapping::get).collect(Collectors.toList());
        List<String> canceledOrderSnList = orderCancelResults.stream().filter(item -> Optional.ofNullable(item).map(OrderCancelResult::getSuccess).orElse(Boolean.FALSE) && EnumUtils.eq(OrderStatusEnum.CANCEL, item.getOrderStatus()))
                .map(OrderCancelResult::getOrderTradeSn).map(orderSnMapping::get).collect(Collectors.toList());
        List<String> cancelingOrderSnList = orderCancelResults.stream().filter(item -> Optional.ofNullable(item).map(OrderCancelResult::getSuccess).orElse(Boolean.FALSE) && EnumUtils.neq(OrderStatusEnum.CANCEL, item.getOrderStatus()))
                .map(OrderCancelResult::getOrderTradeSn).map(orderSnMapping::get).collect(Collectors.toList());
        return CancelOrderOutBO.builder()
                .canceledOrderSnList(canceledOrderSnList)
                .failedOrderSnList(failedOrderSnList)
                .cancelingOrderSnList(cancelingOrderSnList)
                .build();
    }

    /**
     * 获取订单物流信息
     *
     * @param orderSnList   供应链订单号集合
     * @param orderInfoList 订单信息集合
     */
    public List<OrderDeliveryBO> queryLogisticInfoList(List<String> orderSnList, List<OrderInfoBO> orderInfoList,List<LogisticQueryParam.ExpressInfo> expressInfos) {
        checkOrderSn(orderInfoList, OrderInfoBO::getOrderSn, OrderInfoBO::getOrderTradeSn);
        List<LogisticQueryParam> logisticParamList = orderInfoList.stream().map(item -> LogisticQueryParam.builder()
                .channelCode(item.getChannelCode())
                .orderTradeSn(item.getOrderTradeSn())
                .channelOrderSn(item.getChannelOrderSn())
                .channelOrderParentSn(item.getChannelOrderParentSn())
                .channelTradeOrderSn(item.getChannelTradeOrderSn())
				.expressInfo(expressInfos)
                .build()
        ).collect(Collectors.toList());
        //获取物流配送信息
		List<LogisticQueryResult> logisticResultList = Lists.newArrayList();
		try{
			 logisticResultList = supplyChannelTemplate.queryLogisticInfoList(logisticParamList);
		}catch(Exception e){
			log.warn("获取物流配送信息失败入参为:{}", JSON.toJSONString(orderSnList), e);
		}
        if (CollectionUtils.isEmpty(logisticResultList)) {
            return Lists.newArrayList();
        }
        // 订单号转换
        Map<String, String> orderSnMap = orderInfoList.stream().collect(Collectors.toMap(OrderInfoBO::getOrderSn, OrderInfoBO::getOrderTradeSn));

		// 供应链三方交易号物流数据
        Map<String, List<LogisticQueryResult>> logisticResultMap = logisticResultList.stream().collect(Collectors.groupingBy(LogisticQueryResult::getOrderTradeSn));
        List<OrderDeliveryBO> orderDeliveryList = Lists.newArrayList();
        for (String orderSn : orderSnList) {
			String orderTradeSn = orderSnMap.get(orderSn);
			List<LogisticQueryResult> logisticQueryList = logisticResultMap.get(orderTradeSn);
			if (CollectionUtils.isEmpty(logisticQueryList)) {
				continue;
			}

			logisticQueryList = logisticQueryList.stream().filter(Objects::nonNull).filter(item -> CollectionUtils.isNotEmpty(item.getDeliveryInfoList())).collect(Collectors.toList());
            OrderDeliveryBO orderDelivery = this.buildOrderDelivery(orderSn, logisticQueryList);
            orderDeliveryList.add(orderDelivery);
        }
        return orderDeliveryList;
    }

	/**
	 * 批量计算订单运费
	 * @param orderList
	 */
	public BatchCalcOrderFreightResultBO batchCalcOrderFreight(List<OrderBO> orderList) {
		if (log.isDebugEnabled()) {
			log.debug("计算预估运费参数: {}", JSON.toJSONString(orderList));
		}
		List<OrderFreightCalcParam> paramList = orderConverter.toOrderFreightCalcParam(orderList);
		List<OrderFreightCalcResult> orderFreightCalcResults = supplyChannelTemplate.batchCalcOrderFreight(paramList);
		List<BatchCalcOrderFreightResultBO.OrderFreight> orderFreightList = orderConverter.toOrderFreightList(orderFreightCalcResults);
		BatchCalcOrderFreightResultBO batchCalcOrderFreightResult = new BatchCalcOrderFreightResultBO(orderFreightList);
		if (log.isDebugEnabled()) {
			log.debug("计算预估运费结果: {}", JSON.toJSONString(batchCalcOrderFreightResult));
		}
		return batchCalcOrderFreightResult;
	}

    /**
     * 组装物流配送信息
     *
     * @param orderSn           供应链订单号
     * @param logisticQueryList 配送信息
     */
    private OrderDeliveryBO buildOrderDelivery(String orderSn, List<LogisticQueryResult> logisticQueryList) {
        List<OrderDeliveryBO.DeliveryInfo> deliveryInfoList = logisticQueryList.stream().filter(Objects::nonNull).map(logistic -> logistic.getDeliveryInfoList().stream().map(info -> {
            List<LogisticQueryResult.DeliveryInfo.TrackInfo> trackInfoList = info.getTrackInfoList();
            List<OrderDeliveryBO.DeliveryInfo.TrackInfo> trackInfoVoList = Optional.ofNullable(trackInfoList).orElse(Lists.newArrayList()).stream().map(s -> {
                OrderDeliveryBO.DeliveryInfo.TrackInfo trackInfo = new OrderDeliveryBO.DeliveryInfo.TrackInfo();
                trackInfo.setTrackContent(s.getTrackContent());
                trackInfo.setTrackOperator(s.getTrackOperator());
                trackInfo.setTrackMsgTime(s.getTrackMsgTime());
                return trackInfo;
            }).collect(Collectors.toList());
            OrderDeliveryBO.DeliveryInfo deliveryInfo = new OrderDeliveryBO.DeliveryInfo();
            deliveryInfo.setDeliveryOrderSn(info.getDeliveryOrderSn());
            deliveryInfo.setDeliveryCarrier(info.getDeliveryCarrier());
            deliveryInfo.setTrackInfoList(trackInfoVoList);
            return deliveryInfo;
        }).collect(Collectors.toList())).flatMap(Collection::stream).collect(Collectors.toList());

        OrderDeliveryBO orderDelivery = new OrderDeliveryBO();
        orderDelivery.setOrderSn(orderSn);
        orderDelivery.setDeliveryInfoList(deliveryInfoList);
        return orderDelivery;
    }


    /**
     * 检查单号
     * @param data
     * @param orderSnGetter
     * @param orderTradeSnGetter
     * @param <T>
     */
    private <T> void checkOrderSn(List<T> data, Function<T, String> orderSnGetter, Function<T, String> orderTradeSnGetter) {
        if (CollectionUtils.isNotEmpty(data)) {
            for (T obj : data) {
               checkOrderSn(obj, orderSnGetter, orderTradeSnGetter);
            }
        }
    }

    /**
     * 检查单号
     * @param obj
     * @param orderSnGetter
     * @param orderTradeSnGetter
     * @param <T>
     */
    private <T> void checkOrderSn(T obj, Function<T, String> orderSnGetter, Function<T, String> orderTradeSnGetter) {
        if (Objects.nonNull(obj)) {
            if (Objects.nonNull(orderSnGetter)) {
                CustomAssert.hasLength(orderSnGetter.apply(obj), "供应链单号不能为空");
            }
            if (Objects.nonNull(orderTradeSnGetter)) {
                CustomAssert.hasLength(orderTradeSnGetter.apply(obj), "供应链三方交易单号");
            }
        }
    }

	/**
	 * 构建OrderConfirmResultBO
	 * @param orderList
	 * @param orderConfirmResults
	 * @return
	 */
	private OrderConfirmResultBO buildOrderConfirmResult(List<OrderBO> orderList, List<OrderConfirmResult> orderConfirmResults) {
		// 订单号转换
		Map<String, String> orderSnMapping = orderList.stream().collect(Collectors.toMap(OrderBO::getOrderTradeSn, OrderBO::getOrderSn));

		List<OrderConfirmResultBO.ConfirmFailureBO> failedOrderList = orderConfirmResults.stream().filter(item -> !Optional.ofNullable(item).map(OrderConfirmResult::getSuccess).orElse(Boolean.FALSE) || EnumUtils.eq(OrderStatusEnum.FAILURE, item.getStatus()))
				.map(orderConverter::toConfirmFailure).peek(item->item.setOrderSn(orderSnMapping.get(item.getOrderTradeSn()))).collect(Collectors.toList());
		List<OrderConfirmResultBO.ConfirmSuccessBO> confirmSuccessList = orderConfirmResults.stream().filter(item -> Optional.ofNullable(item).map(OrderConfirmResult::getSuccess).orElse(Boolean.FALSE) && EnumUtils.neq(OrderStatusEnum.FAILURE, item.getStatus()))
				.map(orderConverter::toOrderConfirmSuccess).peek(item->item.setOrderSn(orderSnMapping.get(item.getOrderTradeSn()))).collect(Collectors.toList());

		return OrderConfirmResultBO.builder()
				.failedList(failedOrderList)
				.confirmSuccessList(confirmSuccessList)
				.build();
	}


	/**
	 * 合并订单数据
	 * @param orderSplit 拆单后的原始订单数据
	 * @param orderSubmit 提交渠道后的订单数据
	 */
	private void mergeOrderInfo(OrderSplitBO orderSplit, OrderSubmitBO orderSubmit) {
		List<OrderSubmitBO.OrderInfo> orderInfoList = orderSubmit.getOrderInfoList();
		if (CollectionUtils.isEmpty(orderInfoList)) {
			return;
		}
		for (OrderSubmitBO.OrderInfo orderInfo : orderInfoList) {
			// 默认供应链订单号同供应链三方交易订单号一致
			orderInfo.setOrderSn(orderInfo.getOrderTradeSn());
			//获取拆单对应的提交订单数据
			OrderSplitBO.OrderSplitSkuInfo orderSplitSkuInfo = orderSplit.getOrderSkuInfoList().stream().filter(item -> Objects.equals(item.getOrderSn(),orderInfo.getOrderSn())).findFirst().orElse(null);
			CustomAssert.notNull(orderSplitSkuInfo, "拆单信息为空");
			orderInfo.setChannelCode(orderSplitSkuInfo.getChannelCode());
			orderInfo.setOrderType(orderSplitSkuInfo.getOrderType());
			orderInfo.setElectricityFeesInfo(orderSplitSkuInfo.getElectricityFeesInfo());
			orderInfo.setConsigneeInfo(orderSplitSkuInfo.getConsigneeInfo());
			orderInfo.setRecipientInfo(orderSplitSkuInfo.getRecipientInfo());
			for (OrderSubmitBO.OrderInfo.SkuInfo skuInfo : orderInfo.getSkuInfos()) {
				for (OrderSplitBO.SplitSkuInfo splitSkuInfo : orderSplitSkuInfo.getSkuInfoList()) {
					Boolean channelSpecIdCheck  = StringUtils.isNotEmpty(skuInfo.getChannelSpecId()) && StringUtils.isNotEmpty(splitSkuInfo.getChannelSpecId()) ? Objects.equals(skuInfo.getChannelSpecId(),splitSkuInfo.getChannelSpecId()) : Boolean.TRUE;
					Boolean channelProductCodeCheck  = StringUtils.isNotEmpty(skuInfo.getChannelProductCode()) && StringUtils.isNotEmpty(splitSkuInfo.getChannelProductCode()) ? Objects.equals(skuInfo.getChannelProductCode(),splitSkuInfo.getChannelProductCode()) : Boolean.TRUE;
					Boolean channelSkuCodeCheck  = StringUtils.isNotEmpty(skuInfo.getChannelSkuCode()) && StringUtils.isNotEmpty(splitSkuInfo.getChannelSkuCode()) ? Objects.equals(skuInfo.getChannelSkuCode(),splitSkuInfo.getChannelSkuCode()) : Boolean.TRUE;
					if (channelSpecIdCheck && channelProductCodeCheck && channelSkuCodeCheck) {
						skuInfo.setProductType(splitSkuInfo.getProductType());
						skuInfo.setCanInvoice(splitSkuInfo.getCanInvoice());
						skuInfo.setBrandId(splitSkuInfo.getBrandId());
						skuInfo.setBrandName(splitSkuInfo.getBrandName());
						skuInfo.setChannelCode(splitSkuInfo.getChannelCode());
						skuInfo.setChannelProductCode(splitSkuInfo.getChannelProductCode());
						skuInfo.setChannelSpecId(splitSkuInfo.getChannelSpecId());
						skuInfo.setChannelNakedPrice(splitSkuInfo.getChannelNakedPrice());
						skuInfo.setChannelSupplierId(splitSkuInfo.getChannelSpecId());
						skuInfo.setInsideStatus(splitSkuInfo.getInsideStatus());
						skuInfo.setNeedVerifyIdentity(splitSkuInfo.getNeedVerifyIdentity());
						skuInfo.setOriginalPrice(splitSkuInfo.getOriginalPrice());
						skuInfo.setSkuPic(splitSkuInfo.getSkuPic());
						skuInfo.setSaleAttributes(splitSkuInfo.getSaleAttributes());
						skuInfo.setThirdClassifyId(splitSkuInfo.getThirdClassifyId());
						skuInfo.setThirdClassifyName(splitSkuInfo.getThirdClassifyName());
						skuInfo.setUnit(splitSkuInfo.getUnit());
						skuInfo.setRepositoryType(splitSkuInfo.getRepositoryType());
						skuInfo.setPlatformSellPrice(splitSkuInfo.getPlatformSellPrice());
						skuInfo.setPlatformPrice(skuInfo.getSkuPrice());
						skuInfo.setProductCode(splitSkuInfo.getProductCode());
						skuInfo.setSkuTaxRate(splitSkuInfo.getSkuTaxRate());
						skuInfo.setTemplateJson(splitSkuInfo.getTemplateJson());
						skuInfo.setOrderProductParamExtendInfo(splitSkuInfo.getOrderProductParamExtendInfo());
						skuInfo.setSkuCode(splitSkuInfo.getSkuCode());
					}
				}
			}
		}
	}

	/**
	 *  生成提交订单参数参数
	 * @param orderSplit
	 * @param orderRemark
	 * @return
	 */
	private List<OrderSubmitParam.OrderInfo> buildSubmitOrderInfoParam(OrderSplitBO orderSplit, String orderRemark) {
		return orderSplit.getOrderSkuInfoList().stream().map(item -> {
			OrderSubmitParam.OrderInfo orderInfo = new OrderSubmitParam.OrderInfo();
			orderInfo.setOrderTradeSn(item.getOrderTradeSn());
			orderInfo.setChannelCode(item.getChannelCode());
			orderInfo.setRechargeAccount(item.getRechargeAccount());
			orderInfo.setAddressInfoParam(orderConverter.toSubmitOrderAddressInfoParam(item.getConsigneeInfo()));
			orderInfo.setRecipientInfoParam(orderConverter.toSubmitOrderRecipientInfoParam(item.getRecipientInfo()));
			orderInfo.setOrderRemark(orderRemark);

			List<SkuInfoParam> orderSubmitSkuInfoList = item.getSkuInfoList().stream().map(splitSkuInfo -> {
				SkuInfoParam skuInfoParam = new SkuInfoParam();
				skuInfoParam.setSkuNum(splitSkuInfo.getSkuNum());
				skuInfoParam.setSkuCode(splitSkuInfo.getSkuCode());
				skuInfoParam.setChannelSkuName(splitSkuInfo.getSkuName());
				skuInfoParam.setProductType(splitSkuInfo.getProductType());
				skuInfoParam.setChannelSkuCode(splitSkuInfo.getChannelSkuCode());
				skuInfoParam.setChannelProductCode(splitSkuInfo.getChannelProductCode());
				skuInfoParam.setProductCode(splitSkuInfo.getProductCode());
				skuInfoParam.setChannelSpecId(splitSkuInfo.getChannelSpecId());
				skuInfoParam.setRechargeAccount(item.getRechargeAccount());
				skuInfoParam.setChannelNakedPrice(splitSkuInfo.getChannelNakedPrice());
				skuInfoParam.setChannelSkuUnitPrice(splitSkuInfo.getPlatformPrice());
				skuInfoParam.setChannelMarketPrice(splitSkuInfo.getOriginalPrice());
				skuInfoParam.setNeedVerifyIdentity(splitSkuInfo.getNeedVerifyIdentity());
				return skuInfoParam;
			}).collect(Collectors.toList());
			orderInfo.setSkuInfoParamList(orderSubmitSkuInfoList);
			return orderInfo;
		}).collect(Collectors.toList());
	}
}
